package com.payment.web.controller.app;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.base.AjaxResult;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.Remind;
import com.payment.system.service.IRemindService;

@Controller
@RequestMapping("/app/remind")
public class AppRemindController {

	@Autowired
	private IRemindService  remindService;
	
	
	@RequestMapping("/saveRemind")
	@ResponseBody
	public AjaxResult saveRemind(Remind remind) {
		remind.setId(StringUtils.randomUUID());
		if (remind.getType().equals(1)) {
			Calendar sday = Calendar.getInstance();
			sday.add(Calendar.MONTH, -Integer.parseInt(remind.getRepaymentDay()));
			sday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(remind.getRepaymentDay()));
			sday.set(Calendar.HOUR_OF_DAY, 0);
			sday.set(Calendar.MINUTE, 0);
			sday.set(Calendar.SECOND, 0);
			sday.set(Calendar.MILLISECOND, 0);
			Calendar eday = Calendar.getInstance();
			eday.add(Calendar.MONTH, -Integer.parseInt(remind.getRepaymentDay()));
			eday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(remind.getRepaymentDay()));
			eday.set(Calendar.HOUR_OF_DAY, 0);
			eday.set(Calendar.MINUTE, 0);
			eday.set(Calendar.SECOND, 0);
			eday.set(Calendar.MILLISECOND, 0);
			remind.setStartTime(sday.getTime());
			remind.setEndTime(eday.getTime());
			remind.setAlreadyNumber(remind.getTotalNumber()-remind.getSurplusNumbr());
		} else {
			List<Remind> li =	remindService.selctRemindByCardId(remind.getCardId());
			if (li!=null && li.size()>0){
				return AjaxResult.error("该卡已有账单信息，无需重复添加");
			}
		}
		int num = remindService.insertRemind(remind);
		if (num >0 ){
			return AjaxResult.success();
		} else {
			return AjaxResult.error("添加失败");
		}
	}
	
	@RequestMapping("/getList")
	@ResponseBody
	public AjaxResult getList(Integer type,String merchantId) {
		return AjaxResult.successAndData("成功", remindService.selectRemindListByType(type,merchantId));
	}
	
	@RequestMapping("/deleteRemind")
	@ResponseBody
	public AjaxResult deleteRemind(String id) {
		return AjaxResult.successAndData("成功", remindService.deleteRemindByIds(id));
	}
	
	@RequestMapping("/updateRemind")
	@ResponseBody
	public AjaxResult updateRemind(Remind remind) {
		return AjaxResult.successAndData("成功", remindService.updateRemind(remind));
	}
	
}
